﻿using System;
using System.Text.RegularExpressions;

namespace Gonte.Validation
{
    public class FieldRegexValidator
        : FieldValidator
    {
        private const string RegexNoMatch = "Regular expression validator failed for value: {0}";

        public FieldRegexValidator(string pattern, RegexOptions options)
        {
            regularExpression_ = new Regex(pattern, options);
        }
   
        public override bool Validate(Object value)
        {
            if (value == null)
            {
                return true; // We don't validate nulls here
            }

            if (!regularExpression_.IsMatch(value.ToString()))
            {
                error_ = string.Format(RegexNoMatch, value);
                return false;
            }

            return true;
        }

        private Regex regularExpression_;
    }
}
